package org.example.liushichuanshu;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

public class MyByteToStringDecoder extends ByteToMessageDecoder {
    @Override
    protected void decode(ChannelHandlerContext context, ByteBuf in, List<Object> out) throws Exception {
        if (in.readableBytes() < 4) {
            //如果可读字节数小于4个字节，不进行解码，直接返回
            return;
        }
        byte[] messageBytes = new byte[4];
        in.readBytes(messageBytes);
        out.add(new String(messageBytes, StandardCharsets.UTF_8));

    }
}
